//
//  QBPopView.m
//  Qianbao
//
//  Created by liyan1 on 13-8-20.
//  Copyright (c) 2013年 qianwang365. All rights reserved.
//

#import "MEPopView.h"
#define LEFTCONST ((ScreenWidth -255)/2.0)
#define __IMAGENAMED__(__imagename__)                   [UIImage imageNamed:__imagename__]

@interface MEPopView()<UIGestureRecognizerDelegate>
{
    MEPopViewBlock      _saveblock;
    MEPopViewBlock      _cancelBlock;
}
@property (nonatomic, assign) BOOL                       animated;

@property (nonatomic, retain) UIImageView               *iconView;

@property (nonatomic, retain) UIImageView               *titleBackgroundView;
@property (nonatomic, retain) UILabel                   *titleLabel;

@property (nonatomic, retain) UIImageView               *bottomBackgroundView;
@property (nonatomic, retain) UIButton                  *saveBtn;
@property (nonatomic, retain) UIButton                  *cancelBtn;

@property (nonatomic, retain) UIView                    *contentViewBG;
@property (nonatomic, retain) UITapGestureRecognizer    *singleTapGestureRecognizer;
@property (nonatomic, retain) UIView                    *popSubView;
@end

@implementation MEPopView

- (void)dealloc
{

    self.singleTapGestureRecognizer = nil;
}

- (void)cancelALL
{
    [self removeFromSuperview];
}


- (id)initWithTitle:(NSString *)title
          saveTitle:(NSString *)saveTitle
        cancelTitle:(NSString *)cancelTitle
             height:(float)height
{
    return [self initWithTitle:title icon:nil saveTitle:saveTitle cancelTitle:cancelTitle height:height inView:nil];
    
}

- (id)initWithTitle:(NSString *)title
               icon:(UIImage *)icon
          saveTitle:(NSString *)saveTitle
        cancelTitle:(NSString *)cancelTitle
             height:(float)height
{
   return [self initWithTitle:title icon:icon saveTitle:saveTitle cancelTitle:cancelTitle height:height inView:nil];
}

- (id)initWithTitle:(NSString *)title
          saveTitle:(NSString *)saveTitle
        cancelTitle:(NSString *)cancelTitle
             height:(float)height
             inView:(UIView *)view
{
     return [self initWithTitle:title icon:nil saveTitle:saveTitle cancelTitle:cancelTitle height:height inView:view];
}
- (id)initWithTitle:(NSString *)title
               icon:(UIImage *)icon
          saveTitle:(NSString *)saveTitle
        cancelTitle:(NSString *)cancelTitle
             height:(float)height
             inView:(UIView *)view
{
    self = [self init];
    if(self)
    {
        self.contentHeight = height;
        
        self.popSubView = view;
        if(self.popSubView == nil)
        {
            self.popSubView = [UIApplication sharedApplication].keyWindow;
        }

        
        self.backgroundColor    = [UIColor clearColor];
        
        CGRect winframe = self.popSubView.frame;
        
        [self setFrame:CGRectMake(0, 0, winframe.size.width, winframe.size.height)];
        
        UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, winframe.size.width, winframe.size.height)];
        [self addSubview:bg];
        [bg setBackgroundColor:[UIColor blackColor]];
        [bg setAlpha:0.72];
        self.touchOtherHide     = NO;
        
        if(title == nil)
        {
             [self.titleLabel setText:@""];
        }
        else
        {
             [self.titleLabel setText:title];
        }
       
        [self.iconView   setImage:icon];
        if(saveTitle)
        {
            [self.saveBtn setTitle:saveTitle   forState:UIControlStateNormal];
            [self.saveBtn setHidden:NO];
        }
        else
        {
            [self.saveBtn setHidden:YES];
        }
        if(cancelTitle)
        {
            [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
            [self.cancelBtn setHidden:NO];
        }
        else
        {
            [self.cancelBtn setHidden:YES];
        }
        [self.contentView setFrame:CGRectMake(0, 0, 255, height)];
        if(saveTitle == nil && cancelTitle == nil)
        {
            self.contentView.layer.cornerRadius = 6;
            [self.contentView setClipsToBounds:YES];
            [self.contentViewBG setFrame:CGRectMake(0, 0, 255, height/2)];
        }
        else
        {
            [self.contentViewBG setHidden:YES];
        }
        
    }
    return self;
}
- (void)setContentBlackColor:(UIColor *)contentBlackColor
{
    [self.contentView setBackgroundColor:contentBlackColor];
    [self.contentViewBG setBackgroundColor:contentBlackColor];
}

- (void)setSaveblock:(MEPopViewBlock)saveblock cancelBlock:(MEPopViewBlock)cancelBlock
{
    [self _setSaveblock:saveblock];
    
    [self _setCancelBlock:cancelBlock];
}

- (id)init
{
    self = [super init];
    if (self)
    {
        
    }
    return self;
}

- (UIImageView *)iconView
{
    if(!_iconView)
    {
        _iconView = [[UIImageView alloc]initWithImage:nil];
        [self.titleBackgroundView addSubview:_iconView];
        [_iconView setFrame:CGRectMake(15, 10, 30, 30)];
    }
    return _iconView;
}

- (UIImageView *)titleBackgroundView
{
    if(!_titleBackgroundView)
    {
        _titleBackgroundView = [[UIImageView alloc]initWithImage:[self titleBackgroundImage]];
        [self addSubview:_titleBackgroundView];
    }
    return _titleBackgroundView;
}



- (UILabel *)titleLabel
{
    if(!_titleLabel)
    {
        _titleLabel = [[UILabel alloc]init];
        [_titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_titleLabel setTextColor:[self titleColor]];
//        [_titleLabel setShadowColor:[self titleShadowColor]];
        [_titleLabel setShadowOffset:CGSizeMake(0.5, -1)];
        [_titleLabel setFrame:CGRectMake(5, 17, 255, 18)];
        [_titleLabel setTextAlignment:NSTextAlignmentLeft];
//        _CLEAR_BACKGROUND_COLOR_(_titleLabel);
        _titleLabel.backgroundColor = [UIColor clearColor];
        [self.titleBackgroundView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIImageView *)bottomBackgroundView
{
    if(!_bottomBackgroundView)
    {
        _bottomBackgroundView = [[UIImageView alloc]initWithImage:[self bottomBackgroundImage]];
        [self addSubview:_bottomBackgroundView];
        [_bottomBackgroundView setUserInteractionEnabled:YES];
    }
    return _bottomBackgroundView;
}

- (UIButton *)saveBtn
{
    if(!_saveBtn)
    {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom] ;
        [_saveBtn setFrame:CGRectMake(15, 10, 103, 33)];
        [_saveBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [_saveBtn setBackgroundImage:[[self saveBtnImage] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forState:UIControlStateNormal];
        [self.bottomBackgroundView addSubview:_saveBtn];
        [_saveBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}


- (UIButton *)cancelBtn
{
    if(!_cancelBtn)
    {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setFrame:CGRectMake(15+103+16, 10, 103, 33)];
        [_cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [_cancelBtn setBackgroundImage:[[self cancelBtnImage] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)] forState:UIControlStateNormal];
        [self.bottomBackgroundView addSubview:_cancelBtn];
        [_cancelBtn setTitleColor:HEXCOLOR(0x777777) forState:UIControlStateNormal];
        [_cancelBtn setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

- (UIImage *)titleBackgroundImage
{
    return __IMAGENAMED__(@"alert_top_bg.png");
}

- (UIImage *)bottomBackgroundImage
{
    return __IMAGENAMED__(@"alert_bottom_bg.png");
}

- (UIImage *)saveBtnImage
{
    return __IMAGENAMED__(@"bg_button_red.png");
}


- (UIImage *)cancelBtnImage
{
    return __IMAGENAMED__(@"bg_button_gray.png");
}

- (UIColor *)titleColor
{
    return [UIColor blackColor];
}
- (UIColor *)titleShadowColor
{
    return HEXCOLOR(0x6c3150);
}

- (void)saveAction
{
#if NS_BLOCKS_AVAILABLE
	if(_saveblock){
		_saveblock(self);
	}
#endif
}

- (void)cancelAction
{
#if NS_BLOCKS_AVAILABLE
	if(_cancelBlock){
		_cancelBlock(self);
	}
#endif
}

- (UIView *)contentView
{
    if(!_contentView)
    {
        _contentView = [[UIView alloc]init];
        [self contentViewBG];
        [self addSubview:_contentView];
    }
    return _contentView;
}

- (UIView *)contentViewBG
{
    if(!_contentViewBG)
    {
        _contentViewBG = [[UIView alloc]init];
        [self addSubview:_contentViewBG];
    }
    return _contentViewBG;
}


#if NS_BLOCKS_AVAILABLE

- (void)_setSaveblock:(MEPopViewBlock)saveblock
{
    _saveblock = [saveblock copy];
}

- (void)_setCancelBlock:(MEPopViewBlock)cancelBlock
{
    _cancelBlock = [cancelBlock copy];
}

#endif


- (void)setTouchOtherHide:(BOOL)touchOtherHide
{
    _touchOtherHide = touchOtherHide;
    if(_touchOtherHide)
    {
        if(self.singleTapGestureRecognizer == nil)
        {
            self.singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
            self.singleTapGestureRecognizer.delegate = self;
            self.singleTapGestureRecognizer.numberOfTapsRequired = 1;
            [self addGestureRecognizer:self.singleTapGestureRecognizer];
            self.singleTapGestureRecognizer.cancelsTouchesInView = NO;
        }
    }
    else
    {
        if(self.singleTapGestureRecognizer)
        {
            [self removeGestureRecognizer:self.singleTapGestureRecognizer];
        }
    }
    
}

- (void)show:(BOOL)animated
{
    
    if(self.saveBtn.hidden == NO && self.cancelBtn.hidden == NO)
    {
        [self.saveBtn setFrame:CGRectMake(15, 10, 103, 33)];
        [self.cancelBtn setFrame:CGRectMake(15+103+16, 10, 103, 33)];
    }
    else if(self.saveBtn.hidden == YES && self.cancelBtn.hidden == NO)
    {
        [self.cancelBtn setFrame:CGRectMake(15, 10, 103 + 16 + 103, 33)];
    }
    else if(self.saveBtn.hidden == NO && self.cancelBtn.hidden == YES)
    {
        [self.saveBtn setFrame:CGRectMake(15, 10, 103 + 16 + 103, 33)];
    }
    else if(self.saveBtn.hidden == YES && self.cancelBtn.hidden == YES)
    {
        [self.bottomBackgroundView setHidden:YES];
    }
    
    CGRect contframe = self.contentView.bounds;
    
    int h = contframe.size.height/2;
    CGRect winframe = self.popSubView.frame;
    int y = winframe.size.height/2 - h -51;
    [self.titleBackgroundView setFrame:CGRectMake(LEFTCONST, y, 255, 51)];
    [self.contentView setFrame:CGRectMake(LEFTCONST, y+51, contframe.size.width, contframe.size.height)];
    [self.contentViewBG setFrame:CGRectMake(LEFTCONST, y+51, contframe.size.width, contframe.size.height/2)];
    [self.bottomBackgroundView setFrame:CGRectMake(LEFTCONST, CGRectGetMaxY(self.contentView.frame), 255, 54)];
    
    if (animated)
    {
        self.alpha = 0.0;
        [self.popSubView addSubview:self];
        [UIView animateWithDuration:0.6
                         animations:^{
                             self.alpha = 1.0;
                         }];
    }
    else
    {
        [self.popSubView addSubview:self];
    }
}

- (void)moveUpInset:(float)upInset;
{
    CGRect contframe = self.contentView.bounds;
    CGRect contentViewframe = self.contentView.frame;
    int h = contframe.size.height/2;
    CGRect winframe = self.popSubView.frame;
    int y = winframe.size.height/2 - h -51;
    
    [UIView animateWithDuration:0.2
                     animations:^{
                         [self.titleBackgroundView setFrame:CGRectMake(LEFTCONST, y - upInset, 255, 51)];
                         [self.contentView setFrame:CGRectMake(LEFTCONST, y+51 - upInset, contframe.size.width, contframe.size.height)];
                         [self.contentViewBG setFrame:CGRectMake(LEFTCONST, y+51 - upInset, contframe.size.width, contframe.size.height/2)];
                         [self.bottomBackgroundView setFrame:CGRectMake(LEFTCONST, CGRectGetMaxY(contentViewframe) - upInset, 255, 54)];
                     }];
}

- (void)hide:(BOOL)animated
{
    
    [self.layer removeAllAnimations];
    if (animated)
    {
        [UIView animateWithDuration:0.6
                         animations:^{
                             self.alpha = 0.0;
                         }
                         completion:^(BOOL finished) {
                             [self removeFromSuperview];
                         }];
    }
    else
    {
        [self removeFromSuperview];
    }
}

#pragma mark - UIGestureRecognizer delegate

- (void)handleTapGesture:(UIGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateRecognized)
    {
        [self hide:self.animated];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isDescendantOfView:self.contentView])
    {
        return NO;
    }
    if([touch.view isKindOfClass:[UIButton class]])
    {
        return NO;
    }
    return YES;
    
}

@end
